﻿//
//  Copyright 2011  comphilip
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
using System;
using System.Globalization;

namespace Dict4Engkoo.Result {
	/// <summary>
	/// Represents a parameter of DICT reponse
	/// </summary>
	internal abstract class StatusArgument {
		/// <summary>Create a interger status argument</summary>
		/// <param name="Integer">Interger argument</param>
		/// <returns> A <see cref="StatusArgument"/></returns>
		public static StatusArgument CreateIntegerArgument(int Integer){
			return new IntegerArgument(Integer);
		}
		
		/// <summary>Create raw string status argument</summary>
		/// <param name="StringArgument">string content</param>
		/// <returns> A <see cref="StatusArgument"/></returns>
		/// <remarks>String content should not contain spaces</remarks>
		public static StatusArgument CreateRawStringArgument(string StringArgument){
			return new RawStringArgument(StringArgument);
		}
		
		/// <summary>Create quote string status argument</summary>
		/// <param name="StringArgument">string content</param>
		/// <returns> A <see cref="StatusArgument"/></returns>
		/// <remarks>String content could contain spaces and will wrap in double quote</remarks>
		public static StatusArgument CreateQuoteStringArgument(string StringArgument){
			return new QuoteStringArgument(StringArgument);
		}
		
		/// <summary>
		/// Gets formated string to sent to dict client 
		/// </summary>
		public abstract string FormatString{get;}
		
		public override string ToString()
		{
			return FormatString;
		}
		
		private class IntegerArgument : StatusArgument{
			private int IntArgument {get;set;}
			
			public IntegerArgument(int IntArgument){
				this.IntArgument = IntArgument;
			}
			
			public override string FormatString {
				get {
					return IntArgument.ToString();
				}
			}
		}
		
		private class RawStringArgument : StatusArgument{
			private string RawString{get;set;}
			
			public RawStringArgument(string RawString){
				Utility.ArgumentValidator.ValidateEmptyStringArgument(RawString, "RawString");
				this.RawString = RawString;
			}
			
			public override string FormatString {
				get {
					return RawString;
				}
			}
		}
		
		private class QuoteStringArgument : StatusArgument{
			private string StringArgument {get;set;}
			
			public QuoteStringArgument(string StringArgument){
				Utility.ArgumentValidator.ValidateEmptyStringArgument(StringArgument, "StringArgument");
				this.StringArgument = StringArgument;
			}
			
			public override string FormatString {
				get {
					return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", StringArgument.Replace("\"","\\\""));
				}
			}
		}
	}
}
